package ynu.elm.Dao;
/**
 * 商家类
 * 实现功能：
 * 1.商家信息-SQL
 * 2.修改商家信息-SQL
 * 3.更新密码-SQL
 * 4.所属商品管理-SQL
 * 5.退出系统 //集成在loadOn里面
 */
import tools.utils;
import ynu.elm.entities.User;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class ClientUserDao extends User {
    public ClientUserDao(User user){
        MenuDao menu = new MenuDao();
        Scanner sc = new Scanner(System.in);
        while (true) {
            menu.showMenu("商家一级");
            System.out.print("请输入选择：");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    showCtInfo(10020);
                    break;
                case 2:
                    updateInfo(10020);
                    break;
                case 3:
                    updatePwd();
                    break;
                case 4:
                    manageShop();
                    break;
                case 5:
                    System.exit(0);
                default:
                    System.out.println("输入有误！");
                    break;
            }
        }
    }
    public void showCtInfo(int id){
        try {
            //首先连接数据库
            this.getResource().setCon(utils.getConnection());
            String sql="SELECT * FROM merchant WHERE `id` = ?";
            PreparedStatement preparedStatement = this.getResource().getCon().prepareStatement(sql);
            preparedStatement.setInt(1,id);
            this.getResource().setRs(preparedStatement.executeQuery());
            while(this.getResource().getRs().next()){
                System.out.println("商家编号："+this.getResource().getRs().getObject("id"));
                System.out.println("商家名称："+this.getResource().getRs().getObject("name"));
                System.out.println("商家地址："+this.getResource().getRs().getObject("address"));
                System.out.println("商家介绍："+this.getResource().getRs().getObject("introduce"));
                System.out.println("起送费："+this.getResource().getRs().getObject("sendfee"));
                System.out.println("配送费："+this.getResource().getRs().getObject("deliverycost"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            utils.release(this.getResource().getRs(),this.getResource().getSt(),this.getResource().getCon());
        }
    }
    public void updateInfo(int id) {
        String name=null, address=null, introduce=null;
        float sendfee=0,deliverycost=0;
        try {
            int locname=-1,locadd=-1,locintro=-1,locsendfee=-1,locdifee=-1;
            int cnt=0;
            Scanner sc = new Scanner(System.in);
            String ans="n";
            showCtInfo(id);
            String sql="UPDATE `merchant` SET ";
            System.out.println("是否修改商家名称(y/n)：");
            ans=sc.next();
            if(ans.equals("y")){
                sql=sql+"`name`=?,";
                System.out.println("请输入新的商家名称：");
                name=sc.next();
                locname=++cnt;
            }
            System.out.println("是否修改商家地址(y/n)：");
            ans=sc.next();
            if(ans.equals("y")){
                sql=sql+"`address`=?,";
                System.out.println("请输入新的商家地址：");
                address=sc.next();
                locadd=++cnt;
            }
            System.out.println("是否修改商家介绍(y/n)：");
            ans=sc.next();
            if(ans.equals("y")){
                sql=sql+"`introduce`=?,";
                System.out.println("请输入新的商家介绍：");
                introduce=sc.next();
                locintro=++cnt;
            }
            System.out.println("是否修改起送费(y/n)：");
            ans=sc.next();
            if(ans.equals("y")){
                sql=sql+"`sendfee`=?,";
                System.out.println("请输入新的起送费：");
                sendfee=sc.nextFloat();
                locsendfee=++cnt;
            }
            System.out.println("是否修改配送费(y/n)：");
            ans=sc.next();
            if(ans.equals("y")){
                sql=sql+"`deliverycost`=? ";
                System.out.println("请输入新的配送费：");
                deliverycost=sc.nextFloat();
                locdifee=++cnt;
            }
            sql=sql+"WHERE `id`=?";
            if(cnt==0) return;
            this.getResource().setCon(utils.getConnection());
            PreparedStatement preparedStatement = this.getResource().getCon().prepareStatement(sql);
            if(locname!=-1) preparedStatement.setString(locname,name);
            if(locadd!=-1) preparedStatement.setString(locadd,address);
            if(locintro!=-1) preparedStatement.setString(locintro,introduce);
            if(locsendfee!=-1) preparedStatement.setFloat(locsendfee,sendfee);
            if(locdifee!=-1) preparedStatement.setFloat(locdifee,deliverycost);
            preparedStatement.setInt(++cnt,id);
            System.out.println(preparedStatement.toString());
            boolean execute = preparedStatement.execute();
            if (!execute) System.out.println("修改商家信息成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            utils.release(this.getResource().getRs(),this.getResource().getSt(),this.getResource().getCon());
        }
    }
    @SuppressWarnings("JpaQueryApiInspection")
    public void updatePwd(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的旧密码：");
        String pwd=sc.next();
        try {
            this.getResource().setCon(utils.getConnection());
            String sql="SELECT * FROM `loadin` WHERE `password` = ?";
            PreparedStatement preparedStatement = this.getResource().getCon().prepareStatement(sql);
            preparedStatement.setString(1,pwd);
            this.getResource().setRs(preparedStatement.executeQuery());
            if(this.getResource().getRs().next()){
                System.out.println("请输入修改后的密码：");
                String newPwd=sc.next();
                sql="UPDATE `loadin` SET `password`=? WHERE `password`=? AND `name`=? AND `identity`=?";
                preparedStatement=this.getResource().getCon().prepareStatement(sql);
                preparedStatement.setString(1,newPwd);
                preparedStatement.setString(2,pwd);
                preparedStatement.setString(3,this.getResource().getRs().getString("name"));
                preparedStatement.setString(4,this.getResource().getRs().getString("identity"));
                preparedStatement.execute();
                System.out.println("密码更新成功！");
            }else{
                System.out.println("密码输入错误！");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            utils.release(this.getResource().getRs(),this.getResource().getSt(),this.getResource().getCon());
        }
    }
    public void manageShop(){
        new Client2UserDao(this);
    }
}
